
package org.openeai.lite.console.actionhandlers;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;
import org.openeai.lite.common.control.BrokerController;
import org.openeai.lite.common.exception.OpenEAIException;
import org.openeai.lite.common.util.Constants;


@ManagedBean(name = "brokerSendMessageActionHandler")
@RequestScoped
public class BrokerSendMessageActionHandler
{

    private static Logger log = Logger.getLogger(BrokerSendMessageActionHandler.class);

    // properties
    private String defaultDetination;

    private String otherDestination;

    private String payload;

    private String isQueue;

    private String isTopic;

    private String message;

    // Default destination for sending messages
    public String getDefaultDestination()
    {
        return defaultDetination;
    }

    public void setDefaultDestination(String defaultDetination)
    {
        this.defaultDetination = defaultDetination;
    }

    public String getOtherDestination()
    {
        return otherDestination;
    }

    public void setOtherDestination(String otherDestination)
    {
        this.otherDestination = otherDestination;
    }

    // Message body/payload
    public String getPayload()
    {
        return payload;
    }

    public void setPayload(String payload)
    {
        this.payload = payload;
    }

    // is destination of type queue?
    public String getIsQueue()
    {
        return isQueue;
    }

    public void setIsQueue(String isQueue)
    {
        this.isQueue = isQueue;
    }

    // is destination of type topic?
    public String getIsTopic()
    {
        return isTopic;
    }

    public void setIsTopic(String isTopic)
    {
        this.isTopic = isTopic;
    }

    // Feedback Message
    public String getMessage()
    {
        return this.message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

    // Send message from form
    public void sendMessage() throws OpenEAIException
    {
        try
        {
            if (getDefaultDestination().equals("true"))
            {
                log.info("Sending message to default -internal- destination.");
                BrokerController.getInstance().sendMessageToTopic(Constants.MQ_UNITY_DEFAULT_TOPIC,
                                getPayload());
                setMessage("Message sent to internal topic " + Constants.MQ_UNITY_DEFAULT_TOPIC);
            }
            else
            {
                if (getIsQueue().equals("true"))
                {
                    log.info("Preparing message to queue " + getOtherDestination()
                                    + " with payload '" + getPayload() + "'");
                    BrokerController.getInstance().sendMessageToQueue(getOtherDestination(),
                                    getPayload());
                    setMessage("Message sent to queue " + getOtherDestination());
                }
                else if (getIsTopic().equals("true"))
                {
                    log.info("Preparing message to topic " + getOtherDestination()
                                    + " with payload '" + getPayload() + "'");
                    BrokerController.getInstance().sendMessageToTopic(getOtherDestination(),
                                    getPayload());
                    setMessage("Message sent to topic " + getOtherDestination());
                }
            }
        }
        catch (Exception e)
        {
            setMessage("Error sending message!");
            log.error(e);
        }
    }
}
